package org.hb0712.sya.tools.database.common;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class HttpUtils {
    public static String sendPostRequestSync(String url, String jsonBody, Map<String, String> headers) 
            throws Exception {
        
        // 创建HttpClient
        HttpClient client = HttpClient.newBuilder()
                .connectTimeout(Duration.ofSeconds(10))
                .build();
        
        // 构建请求头
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .POST(BodyPublishers.ofString(jsonBody))
                .timeout(Duration.ofSeconds(10));
        
        // 添加请求头
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                requestBuilder.header(entry.getKey(), entry.getValue());
            }
        }
        
        // 构建请求
        HttpRequest request = requestBuilder.build();
        
        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
        
        // 返回响应体
        return response.body();
    }
    
    // 发送表单数据
    public static String sendFormData(String url, Map<String, String> formData, 
            Map<String, String> headers) throws Exception {
        
        // 构建表单数据
        StringBuilder formBodyBuilder = new StringBuilder();
        for (Map.Entry<String, String> entry : formData.entrySet()) {
            if (formBodyBuilder.length() > 0) {
                formBodyBuilder.append("&");
            }
            formBodyBuilder.append(entry.getKey())
                          .append("=")
                          .append(java.net.URLEncoder.encode(entry.getValue(), "UTF-8"));
        }
        String formBody = formBodyBuilder.toString();
        
        // 创建HttpClient
        HttpClient client = HttpClient.newBuilder()
                .connectTimeout(Duration.ofSeconds(10))
                .build();
        
        // 构建请求头
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .POST(BodyPublishers.ofString(formBody))
                .timeout(Duration.ofSeconds(10));
        
        // 添加其他请求头
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                requestBuilder.header(entry.getKey(), entry.getValue());
            }
        }
        
        // 构建请求
        HttpRequest request = requestBuilder.build();
        
        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
        
        // 返回响应体
        return response.body();
    }
}
